﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System.Windows.Input;
using WPF_QC.ServiceReference2;
//using WPF_QC.DataService;
//using Microsoft.Office.Interop.Excel;    //NEED TO RESOLVE ASSEMBLY VERSION CONFLICTS


namespace WPF_QC.QA_ViewModel
{
    public class MeasurementReportsViewModel : ViewModelBase, IPageViewModel
    {
        private static DateTime start;
        private static DateTime end;
        private ServiceReference2.Service1Client serviceClient = new ServiceReference2.Service1Client();
        //private DataService.DataServiceClient serviceClient = new DataService.DataServiceClient();

        public MeasurementReportsViewModel()
        {
            //refreshData(); // FOR USE WITH WCF DATASERVICE
        }

        /* FOR USE WITH WCF DATASERVICE 
        private void refreshData()
        {
            this.serviceClient.GetReportDataCompleted += (s, e) =>
            {
                this.RptData = e.Result;
            };
            this.serviceClient.GetReportDataAsync();
        }
        */

        private Report _currentrptdata;
        public Report CurrentRptData
        {
            get
            {
                return _currentrptdata;
            }

            set
            {
                _currentrptdata = value;
                RaisePropertyChanged("CurrentRptData");
            }

        }

        private DateTime _startDate;
        public DateTime Startdate
        {
            get { return _startDate; }
            set
            {
                _startDate = value;
                RaisePropertyChanged("StartDate");
            }
        }

        private DateTime _endDate;
        public DateTime EndDate
        {
            get { return _endDate; }
            set
            {
                _endDate = value;
                RaisePropertyChanged("EndDate");
            }
        }

        private string _type;
        public string Type
        {
            get { return _type; }
            set
            {
                _type = value;
                RaisePropertyChanged("Type");
            }
        }

        private string _timeFrame;
        public string TimeFrame
        {
            get { return _timeFrame; }
            set
            {
                _timeFrame = value;
                RaisePropertyChanged("TimeFrame");
            }
        }

        public string Name
        {
            get { return "MeasurementReports"; }
        }

        private ICommand _PageParameterCommand;
        public ICommand PageParameterCommand
        {
            get
            {
                if (_PageParameterCommand == null)
                {
                    _PageParameterCommand = new RelayCommand<ButtonCommands>(
                        p => ExecutePageParameterCommand((ButtonCommands)p),
                        CanExecutePageParameterCommand);
                }

                return _PageParameterCommand;
            }
        }

        private IEnumerable<Report> _rptData;
        public IEnumerable<Report> RptData
        {
            get
            {
                return this._rptData;
            }
            set
            {
                this._rptData = value;
                RaisePropertyChanged("RptData");
            }
        }

        private bool CanExecutePageParameterCommand(ButtonCommands bCmd)
        {
            if (bCmd == ButtonCommands.Excel)
                return _rptData != null && _rptData.Count() > 0;
            else
                return true;
        }

        private void ExecutePageParameterCommand(ButtonCommands bCmd)
        {
            if (bCmd == ButtonCommands.Load)
            {
                //DataService.DataServiceClient mr = new DataService.DataServiceClient();
                ServiceReference2.Service1Client mr = new ServiceReference2.Service1Client();
                if (this._timeFrame != "Variable Time Period")
                {
                    switch(this._timeFrame)
                    {
                        case "Previous Week":
                            start = DateTime.Today.AddDays(-7);
                            end = DateTime.Today;
                            break;
                        case "Previous Month":
                            start = DateTime.Today.AddMonths(-1);
                            end = DateTime.Today;
                            break;
                        case "Last 3 Months":
                            start = DateTime.Today.AddMonths(-3);
                            end = DateTime.Today;
                            break;
                    }
                }
                else
                {
                    start = this._startDate;
                    end = this._endDate;
                }

                //RptData = mr.GetReportData().ToList<Report>(); //NOT WORKING
                //RptData = mr.GetReportData(start, end, this._type).ToList<Report>(); //NOT WORKING
              
            }

            /*  NEED TO RESOLVE ASSEMBLY VERSION CONFLICT ERROR BEFORE USING THIS
            if (bCmd == ButtonCommands.Excel)
            {
                Microsoft.Office.Interop.Excel.Application exapp = new Microsoft.Office.Interop.Excel.Application();
                Workbook wb = exapp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
                Worksheet ws = (Worksheet)wb.Worksheets[1];
                Range aRange = ws.get_Range("A1");
                aRange.Value = "Test";
                int iRow;
                int iCol;
                iRow = 1;
                iCol = 1;
                foreach (Report rItem in rptData)
                {
                    iCol = 1;
                    ((Range)ws.Cells[iRow, iCol]).Value  = rItem.MaterialName;
                    iCol += 1;
                    ((Range)ws.Cells[iRow, iCol]).Value = rItem.OnTime;
                    iCol += 1;
                    ((Range)ws.Cells[iRow, iCol]).Value = rItem.Pass_Fail;
                    iCol += 1;
                    ((Range)ws.Cells[iRow, iCol]).Value = rItem.Delayed;
                    iCol += 1;
                    ((Range)ws.Cells[iRow, iCol]).Value = rItem.DateTaken;
                    
                    iRow += 1;
                }
                exapp.Visible = true;
            }*/
        }


    }
}
